﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DAL;
using BLL;
using System.Data;

public partial class Product : System.Web.UI.Page
{
    public int CateId = 44;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCateProduct();
            LoadColor();
            LoadBrand();
        }
        Filter();
        LoadName();
    }

    ProductBLL dataProduct = new ProductBLL();
    ProdCateBLL dataProdCate = new ProdCateBLL();
    ColourBLL dataColor = new ColourBLL();
    BrandCateBLL dataBrandCate = new BrandCateBLL();
    BrandBLL dataBrand = new BrandBLL();

    void LoadName()
    {
        if(Request.QueryString["Id"]!=null)
            CateId = Convert.ToInt32(Request.QueryString["Id"]);
        string CateName = dataProdCate.GetById(CateId).Name;
        ltCateName.Text = CateName;
        ltCateProd.Text = CateName;
    }
    void LoadCateProduct()
    {
        if (Request.QueryString["Id"] != null)
            CateId = Convert.ToInt32(Request.QueryString["Id"]);
        var listProdCate = dataProdCate.GetByParent(CateId);

        DataTable tb = Myfunction.ConvertToDataTable(listProdCate);
        rptCateProduct.DataSource = tb;
        rptCateProduct.DataBind();
    }

    void LoadColor()
    {
        if (Request.QueryString["Id"] != null)
            CateId = Convert.ToInt32(Request.QueryString["Id"]);
        var listColor = dataColor.GetByCateProductId2(CateId);
        DataTable tb = Myfunction.ConvertToDataTable(listColor);
        rptColor.DataSource = tb;

        //rptColor.DataSource = dataColor.GetByCateProductId(CateId);
        rptColor.DataBind();
    }

    void LoadBrand()
    {
        if (Request.QueryString["Id"] != null)
            CateId = Convert.ToInt32(Request.QueryString["Id"]);
        var listBrand = dataBrand.GetByCateParentId(CateId);
        DataTable tb = Myfunction.ConvertToDataTable(listBrand);
        rptBrand.DataSource = tb;
        rptBrand.DataBind();
    }

    void Filter()
    {
        if(Request.QueryString["Id"]!=null)
            CateId = Convert.ToInt32(Request.QueryString["Id"]);
        List<int> listCateId = new List<int>();
        List<int> listColorId = new List<int>();
        List<int> listBrandId = new List<int>();
        foreach (RepeaterItem itemCate in rptCateProduct.Items)
        {
            CheckBox cbCateId = (CheckBox)itemCate.FindControl("cbCateId");
            Label lbCateId = (Label)itemCate.FindControl("lbCateId");
            if(cbCateId.Checked)
                listCateId.Add(int.Parse(lbCateId.Text));
        }

        foreach (RepeaterItem item in rptColor.Items)
        {
            CheckBox cbColorName = (CheckBox)item.FindControl("cbColorName");
            Label lbColorId = (Label)item.FindControl("lbColorId");
            if (cbColorName.Checked)
                listColorId.Add(int.Parse(lbColorId.Text));
        }

        foreach (RepeaterItem item in rptBrand.Items)
        {
            CheckBox cbBrandName = (CheckBox)item.FindControl("cbBrandName");
            Label lbBrandId = (Label)item.FindControl("lbBrandId");
            if (cbBrandName.Checked)
                listBrandId.Add(int.Parse(lbBrandId.Text));
        }

        int Sort = Convert.ToInt32(dropSort.SelectedValue);
        if (listBrandId.Count == 0 && listColorId.Count == 0 && listCateId.Count == 0)
        {
            var listProduct = dataProduct.GetByCateParentId(CateId);
            if (Sort == 1)
                listProduct = listProduct.OrderBy(p => p.Cost).ToList();
            else listProduct = listProduct.OrderByDescending(p => p.Cost).ToList();
            DataTable tb2 = Myfunction.ConvertToDataTable(listProduct);
            clPages.MaxPages = 100000;
            clPages.PageSize = 9;
            clPages.DataSource = tb2.DefaultView;
            clPages.BindToControl = rptProductFilter;

            rptProductFilter.DataSource = clPages.DataSourcePaged;
            //Response.Write(@"<script language='javascript'>$('.filter-search input').addClass('css-checkbox');$('.filter-search label').addClass('css-label lite-orange-check');</script>");
            rptProductFilter.DataBind();
            
        }
        else {
            var listProductFilter = dataProduct.GetByFilter(listCateId, listColorId, listBrandId);
            if (Sort == 1)
                listProductFilter = listProductFilter.OrderBy(p => p.Cost).ToList();
            else listProductFilter = listProductFilter.OrderByDescending(p => p.Cost).ToList();
            DataTable tb = Myfunction.ConvertToDataTable(listProductFilter);
            clPages.MaxPages = 100000;
            clPages.PageSize = 9;
            clPages.DataSource = tb.DefaultView;
            clPages.BindToControl = rptProductFilter;

            rptProductFilter.DataSource = clPages.DataSourcePaged;
            //Response.Write(@"<script language='javascript'>$('.filter-search input').addClass('css-checkbox');$('.filter-search label').addClass('css-label lite-orange-check');</script>");
            rptProductFilter.DataBind();
            
        }
    }
    protected void rptCateProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //CheckBox cbCateId = (CheckBox)e.Item.FindControl("cbCateId");
        //cbCateId.Attributes.Add("class", "thanhcong");
    }
    protected void rptCateProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        HtmlAnchor aCateId = (HtmlAnchor)e.Item.FindControl("aCateId");
    }
    protected void rptProductFilter_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        SizeBLL dataSize = new SizeBLL();
        LabelBLL dataLabel = new LabelBLL();
        Label lbProductID = (Label)e.Item.FindControl("lbId");
        Label lbLabelId = (Label)e.Item.FindControl("lbLabelId");
        string productId = lbProductID.Text.ToString();

        var listSize = dataSize.GetByProductId(productId);
        DataTable tbSize = Myfunction.ConvertToDataTable(listSize);

        Repeater rptSize = (Repeater)e.Item.FindControl("rptSize");
        rptSize.DataSource = tbSize;
        rptSize.DataBind();

        List<LabelSP> listLabel = new List<LabelSP>();
        LabelSP lp = dataLabel.GetById(int.Parse(lbLabelId.Text));
        listLabel.Add(lp);
        DataTable tbLabel = Myfunction.ConvertToDataTable(listLabel);
        Repeater rptLabel = (Repeater)e.Item.FindControl("rptLabel");
        rptLabel.DataSource = tbLabel;
        rptLabel.DataBind();
        
    }
}